import request from "/utils/omsRequest";
const app = getApp();

Component({
  mixins: [],
  data: {
    value: [0, 0, 0]
  },
  props: {
    citys: [],
    show: false,
    onClose: () => { },
    onLoadData: () => { },
    onChange: () => { }
  },
  didMount() {
  },
  didUpdate() { },
  didUnmount() { },
  methods: {
    onCanCel() {
      this.props.onClose();
    },
    onConfirm() {
      const [provinceIndex, cityIndex, areaIndex] = this.data.value;
      let provinceId = this.props.citys[provinceIndex].id;
      let cityId = this.props.citys[provinceIndex].children[cityIndex].id;
      let areaId = this.props.citys[provinceIndex].children[cityIndex].children[areaIndex].id;
      this.props.onChange(this.data.value, [provinceId, cityId, areaId]);
    },
    onPickerChange(e) {
      let [province, city] = e.detail.value;
      let [originProvince, originCity] = this.data.value;
      let value = [];
      if (province !== originProvince) {
        // province chagned
        this.props.onLoadData({
          areaId: this.props.citys[province].id,
          type: this.props.citys[province].type + 1
        }, { provinceIndex: province });
        value = [province, 0, 0]
      } else if (city != originCity) {
        // city changed
        this.props.onLoadData({
          areaId: this.props.citys[province].children[city].id,
          type: this.props.citys[province].children[city].type
        }, { provinceIndex: province, cityIndex: city });
        value = [province, city, 0]
      } else {
        value = e.detail.value
      }
      this.setData({
        value: value
      });
    }
  },
});
